﻿using System;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Linq;
using System.Windows;
using SensorDroneStudio.Services;
using SensorDroneStudio.ViewModels;
using SimpleMvvmToolkit;

namespace SensorDroneStudio.Locators
{
    public class VmLocator
    {
        private static readonly CompositionContainer Container;
        private AgentType _agentType = AgentType.Real;

        static VmLocator()
        {
            var catalog = new AggregateCatalog();
            catalog.Catalogs.Add(new AssemblyCatalog(typeof (VmLocator).Assembly));

            
            Container = new CompositionContainer(catalog);
        }

        public VmLocator()
        {
            Container.ComposeParts(this);
            
        }

        public MainPageViewModel MainPageViewModel
        {
            get { return new MainPageViewModel(); }
        }

        public DroneSelectorViewlModel SelectorViewModel
        {
            get
            {
                var service = Services.FirstOrDefault(s => s.Metadata.AgentType == _agentType);
                return new DroneSelectorViewlModel(service.Value);
            }
        }

        public DroneViewModel DroneViewModel
        {
            get
            {
                return new DroneViewModel();
            }
        }

        public SensorViewModel SensorViewModel
        {
            get
            {
                return new SensorViewModel();
            }
        }

        private static bool IsInDesignMode
        {
            get
            {
                var prop = DesignerProperties.IsInDesignModeProperty;
                var isInDesignMode =
                    (bool) DependencyPropertyDescriptor.FromProperty(prop, typeof (FrameworkElement)).Metadata.DefaultValue;
                return isInDesignMode;
            }
        }

        public AgentType AgentType
        {
            get { return _agentType; }
            set { _agentType = value; }
        }

        [ImportMany]
        Lazy<ISensorDroneService,IServiceAgentMetadata>[] Services { get; set; } 
    }
}